package com.fangngng.pushall.controller.manage;

import com.fangngng.pushall.controller.BaseController;
import com.fangngng.pushall.dto.RequestUserVO;
import com.fangngng.pushall.dto.Result;
import com.fangngng.pushall.entity.User;
import com.fangngng.pushall.service.UserService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author fei.fang
 * @date 2021/4/13
 **/
@Slf4j
@RestController
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @PreAuthorize("hasAuthority('admin')")
    @PostMapping(value = "/user/add")
    public Result<User> add(@RequestBody User user) {
        User userDetail = super.getCurrentUser();
        if (userDetail == null) {
            log.warn("dashboard需要登录");
            return Result.fail("需要登陆");
        }

        return Result.ok(userService.addUser(user));
    }

    @PreAuthorize("hasAuthority('admin')")
    @PostMapping(value = "/user/delete")
    public Result<User> delete(@RequestBody User user) {
        User userDetail = super.getCurrentUser();
        if (userDetail == null) {
            log.warn("dashboard需要登录");
            return Result.fail("需要登陆");
        }

        return Result.ok(userService.deleteUser(user));
    }

    @PreAuthorize("hasAuthority('admin')")
    @PostMapping(value = "/user/edit")
    public Result<User> edit(@RequestBody User role) {
        User userDetail = super.getCurrentUser();
        if (userDetail == null) {
            log.warn("dashboard需要登录");
            return Result.fail("需要登陆");
        }

        return Result.ok(userService.editUser(role));
    }

    @PreAuthorize("hasAuthority('admin')")
    @PostMapping(value = "/user/page")
    public Result<PageInfo<User>> subscribePost(@RequestBody RequestUserVO user) {
        User userDetail = super.getCurrentUser();
        if (userDetail == null) {
            log.warn("dashboard需要登录");
            return Result.fail("需要登陆");
        }

        return Result.ok(userService.findByUser(user));
    }

    // todo user role 增删改查
}
